package com.nativetalk.controller.app.study;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.course.catalog.TdNtCourseCatalog;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealDetail;
import com.nativetalk.bean.study.TdNtStudyTeachToComment;
import com.nativetalk.bean.study.feedback.TdNtStudyTeachFeedbackInset;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.member.course.search.MembCourseSearchService;
import com.nativetalk.service.member.search.MemberSearchService;
import com.nativetalk.service.member.update.MemberUpdateService;
import com.nativetalk.service.membsetmeal.search.MembSetmealSearchService;
import com.nativetalk.service.study.feedback.StudyTeachFeedbackService;
import com.nativetalk.service.study.insert.StudyTeachInsertService;
import com.nativetalk.service.study.search.StudyTeachService;
import com.nativetalk.service.study.update.StudyTeachUpdateService;
import com.nativetalk.service.teacher.search.TeacherSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class StudyController {
    private static Logger logger = Logger.getLogger(StudyController.class);
    @Resource
    private StudyTeachService studyTeachService;
    @Resource
    private StudyTeachFeedbackService studyTeachFeedbackService;
    @Resource
    private StudyTeachInsertService studyTeachInsertService;
    @Resource
    private StudyTeachUpdateService studyTeachUpdateService;
    @Resource
    private MemberSearchService memberSearchService;
    @Resource
    private MembSetmealSearchService membSetmealSearchService;
    @Resource
    private TeacherSearchService teacherSearchService;
    @Resource
    private MembCourseSearchService membCourseSearchService;
    @Resource
    private MemberUpdateService memberUpdateService;


    /**
     * @return RetInfo
     * @Purpose 查看个人学习记录
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo studyTeach(@RequestBody Map<String, String> map,
                              @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            RetInfo retInfo = studyTeachService.findStudyTeachPage(tdNtMember.getMemb_id(), Integer.valueOf(map.get("currentPage")), 10);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查看个人学习记录详情
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/detail", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo detail(@RequestBody Map<String, String> map,
                          @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            RetInfo retInfo = studyTeachService.findStudyTeachDetail(Integer.valueOf(map.get("stt_id")));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 学习疑问反馈
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/feedback", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo feedback(@Valid @RequestBody TdNtStudyTeachFeedbackInset tdNtStudyTeachFeedbackInset, BindingResult result,
                            @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtStudyTeachFeedbackInset));

            if (result.hasErrors()) {
                RetInfo retInfo = new RetInfo();
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
                return retInfo;
            }

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            tdNtStudyTeachFeedbackInset.setUser_cate(1);
            tdNtStudyTeachFeedbackInset.setMemb_id(tdNtMember.getMemb_id());
            RetInfo retInfo = studyTeachFeedbackService.inserttudyTeachFeedback(tdNtStudyTeachFeedbackInset);


            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 跳转到评价页面
     * @version 1.0
     * @time 2015-01-06
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/tocomment", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo tocomment(@RequestBody Map<String, String> map) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo = studyTeachService.findStudyTeach(Integer.valueOf(map.get("stt_id")));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 开始学习
     * @version 1.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/start", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo start(@RequestBody Map<String, String> map,
                         @RequestHeader(value = "token", required = true) String token,
                         @RequestHeader(value = "version", required = true) Integer version) {

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());

            RetInfo retInfo = new RetInfo();
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
            TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(),
                    Integer.valueOf(map.get("cou_id")));
            if (tdNtMembSetmealDetail != null) {
                if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                    retInfo = studyTeachInsertService.inserttdNtMembSetmeal(tdNtMember, map.get("cou_id"), version);
                } else if (balance.doubleValue() >= sutdy_amount) {
                    retInfo = studyTeachInsertService.inserttdNtMembSetmeal(tdNtMember, map.get("cou_id"), version);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                }
            } else {
                if (balance.doubleValue() >= sutdy_amount) {
                    retInfo = studyTeachInsertService.inserttdNtMembSetmeal(tdNtMember, map.get("cou_id"), version);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 一键呼叫
     * @version 1.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/oneKeyCall", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo oneKeyCall(@RequestHeader(value = "token", required = true) String token,
                              @RequestHeader(value = "version", required = true) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());

            logger.info("memb_id=" + tdNtMember.getMemb_id() + "的余额为：" + balance.doubleValue());

            RetInfo retInfo = new RetInfo();
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
            TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(), 0);
            if (tdNtMembSetmealDetail != null) {
                if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                    retInfo = studyTeachInsertService.oneKeyCall(tdNtMember, version);
                } else if (balance.doubleValue() >= sutdy_amount) {
                    retInfo = studyTeachInsertService.oneKeyCall(tdNtMember, version);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                }
            } else {
                if (balance.doubleValue() >= sutdy_amount) {
                    retInfo = studyTeachInsertService.oneKeyCall(tdNtMember, version);
                } else {

                    retInfo.setMark("1");
                    retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 结束教学
     * @version 1.0
     * @time 2015-01-12
     * @author lizhun
     */
    //用不到了
    @RequestMapping(value = "/app/1/token/member/studyTeach/end", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo end(@RequestBody Map<String, Integer> map,
                       @RequestHeader(value = "token", required = true) String token,
                       @RequestHeader(value = "version", required = true) String version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            logger.info("结束学习，memb_id=" + tdNtMember.getMemb_id() + ",stt_id=" + map.get("stt_id"));
            RetInfo retInfo = studyTeachUpdateService.end(map.get("stt_id"), version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 取消
     * @version 1.0
     * @time 2015-01-12
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/cancel", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo cancel(@RequestHeader(value = "token", required = true) String token,
                          @RequestHeader(value = "version", required = true) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            Integer stt_id = (Integer) MemcachedUtils.get(token + "nativetalk");
            MemcachedUtils.delete(token + "nativetalk");

            RetInfo retInfo = studyTeachUpdateService.eceivingOrders(stt_id, 0, 1, version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找用户余额及套餐
     * @version 1.0
     * @time 2015-01-12
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/studyTeach/membSetMeal", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo membSetMeal(@RequestBody Map<String, Integer> map,
                               @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info("查找用户余额及套餐: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            logger.info("memb_id=" + tdNtMember.getMemb_id());

            RetInfo retInfo = membSetmealSearchService.findMembSetmealByMembIdCouId(tdNtMember.getMemb_id(), map.get("cou_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 开始学习
     * @version 2.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/2/token/member/studyTeach/start", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo startTwo(@RequestBody Map<String, String> map,
                            @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info("开始学习: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());
            logger.info("开始学习：memb_id=" + tdNtMember.getMemb_id() + "，余额：" + balance.doubleValue());

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = new RetInfo();
            if (tsNtWebSiteConfIndexMap.get("9").getData_value().equals("0") &&
                    CommonUtil.isInDate(new Date(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value())) {
                retInfo = studyTeachInsertService.inserttdNtMembSetmealTwo(tdNtMember, map.get("cou_id"));
            } else {
                double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
                TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(),
                        Integer.valueOf(map.get("cou_id")));
                if (tdNtMembSetmealDetail != null) {
                    if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealTwo(tdNtMember, map.get("cou_id"));
                    } else if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealTwo(tdNtMember, map.get("cou_id"));
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                } else {
                    if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealTwo(tdNtMember, map.get("cou_id"));
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 开始学习
     * @version 3.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/3/token/member/studyTeach/start", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo startThree(@RequestBody Map<String, String> map,
                              @RequestHeader(value = "token", required = true) String token,
                              @RequestHeader(value = "version", required = true) String version) {
        try {
            //打印入参
            logger.info("开始学习: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());
            logger.info("开始学习：memb_id=" + tdNtMember.getMemb_id() + "，余额：" + balance.doubleValue());

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = new RetInfo();
            double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
            if (tsNtWebSiteConfIndexMap.get("9").getData_value().equals("0") &&
                    CommonUtil.isInDate(new Date(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value())) {
                retInfo = studyTeachInsertService.inserttdNtMembSetmealThree(tdNtMember, map.get("cou_id"), balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
            } else {
                TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(),
                        Integer.valueOf(map.get("cou_id")));
                if (tdNtMembSetmealDetail != null) {
                    if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealThree(tdNtMember, map.get("cou_id"), balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealThree(tdNtMember, map.get("cou_id"), balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                } else {
                    if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.inserttdNtMembSetmealThree(tdNtMember, map.get("cou_id"), balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 一键呼叫
     * @version 2.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/2/token/member/studyTeach/oneKeyCall", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo oneKeyCallTwo(@RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info("一键呼叫: " + Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());
            logger.info("一键呼叫：memb_id=" + tdNtMember.getMemb_id() + "，余额：" + balance.doubleValue());

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = new RetInfo();
            if (tsNtWebSiteConfIndexMap.get("9").getData_value().equals("0") &&
                    CommonUtil.isInDate(new Date(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value())) {
                retInfo = studyTeachInsertService.oneKeyCallTwo(tdNtMember);
            } else {
                double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
                TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(),
                        0);
                if (tdNtMembSetmealDetail != null) {
                    if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                        retInfo = studyTeachInsertService.oneKeyCallTwo(tdNtMember);
                    } else if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.oneKeyCallTwo(tdNtMember);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                } else {
                    if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.oneKeyCallTwo(tdNtMember);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 一键呼叫
     * @version 3.0
     * @time 2015-01-07
     * @author lizhun
     */
    @RequestMapping(value = "/app/3/token/member/studyTeach/oneKeyCall", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo oneKeyCallThree(@RequestHeader(value = "token", required = true) String token,
                                   @RequestHeader(value = "version", required = true) String version) {
        try {
            //打印入参
            logger.info("一键呼叫: " + Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            BigDecimal balance = memberSearchService.findMemberBalanceStudy(tdNtMember.getMemb_id());
            logger.info("一键呼叫：memb_id=" + tdNtMember.getMemb_id() + "，余额：" + balance.doubleValue());

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = new RetInfo();
            double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
            if (tsNtWebSiteConfIndexMap.get("9").getData_value().equals("0") &&
                    CommonUtil.isInDate(new Date(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value())) {
                retInfo = studyTeachInsertService.oneKeyCallThree(tdNtMember, balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
            } else {
                TdNtMembSetmealDetail tdNtMembSetmealDetail = membSetmealSearchService.findMembSetmealByMembIdCouIdTwo(tdNtMember.getMemb_id(),
                        0);
                if (tdNtMembSetmealDetail != null) {
                    if (tdNtMembSetmealDetail.getSurplus_time() > 0) {
                        retInfo = studyTeachInsertService.oneKeyCallThree(tdNtMember, balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.oneKeyCallThree(tdNtMember, balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                } else {
                    if (balance.doubleValue() >= sutdy_amount) {
                        retInfo = studyTeachInsertService.oneKeyCallThree(tdNtMember, balance.doubleValue(), tsNtWebSiteConfIndexMap.get("10").getData_value(), tsNtWebSiteConfIndexMap.get("11").getData_value(), tsNtWebSiteConfIndexMap.get("9").getData_value(), sutdy_amount, version);
                    } else {

                        retInfo.setMark("1");
                        retInfo.setTip("您的余额不足支付一分钟费用,请先充值!");
                    }
                }
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 根据教师id查找教师详情
     * @version 2.0
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/2/token/study/teacher", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacher(@RequestBody Map<String, Integer> mapParam) {
        try {
            //打印入参
            logger.info("根据教师id查找教师详情: " + Constants.prefix_in + JsonUtil.toJson(mapParam));

            RetInfo retInfo = teacherSearchService.findTeacherStudy(mapParam.get("tea_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 跳转到评价页面
     * @version 2.0
     * @time 2015-01-06
     * @author lizhun
     */
    @RequestMapping(value = "/app/2/token/member/studyTeach/tocomment", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo tocommentTwo(@RequestBody Map<String, String> map,
                                @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info("跳转到评价页面: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = studyTeachService.findStudyTeach(Integer.valueOf(map.get("stt_id")));
            TdNtStudyTeachToComment tdNtStudyTeachToComment = (TdNtStudyTeachToComment) retInfo.getObj();
            Map<String, Object> result = new HashMap<>();
            result.put("studyteach", tdNtStudyTeachToComment);
            result.put("share_picutre", tsNtWebSiteConfIndexMap.get("15").getData_value());
            result.put("share", tsNtWebSiteConfIndexMap.get("12").getData_value());
            result.put("share_name", tsNtWebSiteConfIndexMap.get("13").getData_value());
            result.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value() + "?invite_code="
                    + tdNtMember.getMemb_invite_code());
            result.put("share_content", tsNtWebSiteConfIndexMap.get("16").getData_value());
            retInfo.setObj(result);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找是否开启免费时间段
     * @version 2.0
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/2/token/study/free", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo free() {
        try {
            //打印入参
            logger.info("查找是否开启免费时间段");

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("free", tsNtWebSiteConfIndexMap.get("9").getData_value());
            map.put("free_start", tsNtWebSiteConfIndexMap.get("10").getData_value());
            map.put("free_end", tsNtWebSiteConfIndexMap.get("11").getData_value());
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 跳转到评价页面
     * @version 3.0
     * @time 2015-01-06
     * @author lizhun
     */
    @RequestMapping(value = "/app/3/token/member/studyTeach/tocomment", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo tocommentThree(@RequestBody Map<String, String> map,
                                  @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info("跳转到评价页面: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = studyTeachService.findStudyTeachThree(Integer.valueOf(map.get("reserve_id")), tdNtMember.getMemb_id());
            TdNtCommentPage tdNtReserveInsert = (TdNtCommentPage) retInfo.getObj();
            Map<String, Object> result = new HashMap<>();
            result.put("studyteach", tdNtReserveInsert);
            result.put("share_picutre", tsNtWebSiteConfIndexMap.get("15").getData_value());
            result.put("share", tsNtWebSiteConfIndexMap.get("12").getData_value());
            result.put("share_name", tsNtWebSiteConfIndexMap.get("13").getData_value());
            result.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value() + "?invite_code="
                    + tdNtMember.getMemb_invite_code());
            result.put("share_content", tsNtWebSiteConfIndexMap.get("16").getData_value());
            retInfo.setObj(result);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 跳转到打卡
     * @version 3.0
     * @time 2015-01-06
     * @author lizhun
     */
    @RequestMapping(value = "/app/3/token/member/studyTeach/topunchedcart", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo toPunchedCart(@RequestBody Map<String, String> map,
                                  @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info("跳转到打卡页面: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

           // Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            RetInfo retInfo = studyTeachService.toPunchedCart(Integer.valueOf(map.get("reserve_id")), tdNtMember.getMemb_id(),Integer.valueOf(map.get("cou_id")));
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * 打卡接口
     * @param map
     * @param token
     * @return
     */
    @RequestMapping(value = "/app/3/token/member/studyTeach/punchedcart", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo punchedcart(@RequestBody Map<String, String> map,
                                 @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info("去打卡: " + Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;
            if(StringUtils.isBlank(map.get("reserve_id"))){
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("reserve_id 不能为空");
                return retInfo;
            }
            RetInfo retInfo = studyTeachService.punchedCart(map.get("reserve_id"),tdNtMember.getMemb_id());
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 学习中心
     * @version 2.0
     * @time 2016/4/17
     * @author wuyuanhui
     */
    @RequestMapping(value = "/app/3/learn/center", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo learncenter(@RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info("学习中心: " + Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            map.put("video_switch", tdNtMember.getVideo_switch());
            TdNtMembCourseDetail tdNtMembCourseDetail = membCourseSearchService.findTdNtMembCourseDetailByMembId(tdNtMember.getMemb_id());
            if (tdNtMembCourseDetail != null) {
                Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
                TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(tdNtMembCourseDetail.getCou_id() + "");
                map.put("cou_name", tsNtCourseInfoDetail.getCou_name());
                Map<String, List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tdNtMembCourseDetail.getCou_id() + "");
                map.put("picture", tdNtCoursePictureLists.get(0).getImg_url());
                Map<String, List<TdNtCourseCatalog>> tdNtCourseCatalogMap = (Map<String, List<TdNtCourseCatalog>>) MemcachedUtils.get(MemcachedKey.tdNtCourseCatalogMap);
                List<TdNtCourseCatalog> tdNtCourseCatalogs = tdNtCourseCatalogMap.get(tdNtMembCourseDetail.getCou_id() + "");
                int size = 0;
                for (int i = 0; i < tdNtCourseCatalogs.size(); i++) {
                    if (i < (tdNtCourseCatalogs.size() - 1)) {
                        if (tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(i).getEnd_page() && tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(i + 1).getEnd_page()) {
                            size = i + 1;
                            break;
                        }
                    } else {
                        if (tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(i).getEnd_page()) {
                            size = i + 1;
                            break;
                        }
                    }


                }
                map.put("size", size);
                map.put("cou_id", tdNtMembCourseDetail.getCou_id());
                map.put("cate_id", tsNtCourseInfoDetail.getCate_id());
                map.put("totalSize", tdNtCourseCatalogs.size());
                map.put("course", 0);
            } else {
                retInfo.setTip("您还没有开始学习");
                map.put("course", 1);
            }

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setObj(map);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 视频开关
     * @version 1.0
     * @time 2016-4-12
     * @author wuyuanhui
     */
    @RequestMapping(value = "/app/3/learn/center/videoswitch", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo videoswitch(@RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info("学习中心: " + Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            if ("0".equals(tdNtMember.getVideo_switch())) {
                tdNtMember.setVideo_switch("1");
                retInfo = memberUpdateService.updatevideoswitch(tdNtMember);
                map.put("video_switch", "1");
            } else {
                tdNtMember.setVideo_switch("0");
                retInfo = memberUpdateService.updatevideoswitch(tdNtMember);
                map.put("video_switch", "0");
            }
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}


